function [out] = plotFile(FILENAME)
%%%%%% plot the content of a file %%%%%%%

if nargin == 0
    FILENAME = 'result/r1_p3_CTSz1.txt';
%     FILENAME = 'result/test.txt';
end

%[pathstr, fname, ext] = fileparts(FILENAME);
fid = fopen(FILENAME, 'r'); % readin file
A = textscan(fid, '%s', 'CommentStyle', '#', 'Delimiter', '\r');
B = A{1};
isink = -1;
nsink = 0;
rnsink = 0; % real nsink
netxyz = [];
imgp = -1;
mgpxyz = [];
cootype = 'Sink';   % type of coordinates
tiercolor = {'k', 'b', 'r', 'c', 'm', 'y', 'g'};
clksrc = []; % default clk source
tsvxyz = [];
itsv = -1;
ibuf = -1;
bufxyz = [];

Barray = regexp(B, ':|\s', 'split');

% read in IBM suits
for ii=1:length(Barray)
    temp = Barray{ii};
    temp(cellfun(@isempty,temp)) = [];  % remove empty elements
    name = temp{1};
    switch name
        case 'NumSinks'
            nsink = str2double(temp{2});   % # of sinks
            xyz = zeros(nsink, 3);  % coordinate set
            cload = zeros(nsink,1); % capacitive load
        case 'Sink'
            isink = str2double(temp{2});
            cootype = 'Sink';
            rnsink = rnsink+1;
        case 'Net'         
            cootype = 'Net';
        case 'Merge'
            imgp = str2double(temp{3});            
            cootype = 'Merge Point';
        case 'Coordinate'
            switch cootype
                case 'Sink'
                    xyz(isink+1,1:2) = str2double(temp(2:3)); % um
                    if length(temp) == 4
                        xyz(isink+1,3) = str2double(temp(4));
                    end
                case 'Merge Point'
                    if length(temp) == 4
                        % z is included
                        mgpxyz = [mgpxyz; str2double(temp(2:4))]; % um, the ids of mgpxyz is not continous
                    else
                        mgpxyz = [mgpxyz; str2double(temp(2:3)),0]; % um
                    end
                case 'Net'
                    if length(temp) == 6
                        netxyz = [netxyz; str2double(temp(2:6))];
                    else
                        netxyz = [netxyz; [str2double(temp(2:5)), 0]];
                    end
                case 'TSV'
                    tsvxyz(itsv+1,:) = str2double(temp(2:5)); % x, y, z1, z2
                case 'Buffer'
                    bufxyz(ibuf+1,1:2) = str2double(temp(2:3)); % x, y, z
                    if length(temp) == 4
                        % z is included
                        bufxyz(ibuf+1,3) = str2double(temp(4)); % x, y, z
                    end
            end
%         case 'Capacitive'
%             cload(isink+1,:) = str2double(temp{3}); % F
        case 'TSV'
            cootype = 'TSV';
            itsv = str2double(temp{2});
        case 'Source'
            if length(temp) == 3
                % only one clock source
                clksrc = [str2double(temp(2:3)) 1]; 
            else
                clksrc = str2double(temp(2:4)); 
            end
        case 'BUF'
            % similar to merge point
            ibuf = str2double(temp{2});            
            cootype = 'Buffer';
    end
end

% check the number of components
assert(nsink == rnsink, '# sink is wrong!');
assert(size(mgpxyz,1)+nsink+size(tsvxyz,1)+size(bufxyz,1)==size(netxyz,1)+1, '# net is wrong!');
% % % draw figures
% refine x, y
xyz(:,1:2) = xyz(:,1:2)*1e-4;   % the old xy is 0.1um, change to 1mm
netxyz(:,1:4) = netxyz(:,1:4)*1e-4;   % the old xy is 0.1um, change to 1mm
tsvxyz(:,1:2) = tsvxyz(:,1:2)*1e-4;   % the old xy is 0.1um, change to 1mm
if size(bufxyz,1) > 0
    bufxyz(:,1:2) = bufxyz(:,1:2)*1e-4;   % the old xy is 0.1um, change to 1mm
end
clksrc(1:2) = clksrc(1:2)*1e-4;
% define the axes
xymin = min([min(xyz); clksrc]);
xymax = max([max(xyz); clksrc]);
border = (xymax-xymin)/100; % x, y shouldn't exceed sinks' coordinates
xystart = xymin-border;
xyend = xymax+border;
% Ha_axes = axes('xlim', [0 max(max(X))*1.1], 'ylim', [0 max(max(Y))*1.1], 'ztick', (1:1:max(Z)), 'dataaspectratio', [1 1 0.4], 'fontsize', 12);
Ha_axes = axes('xlim', [xystart(1) xyend(1)], 'ylim', [xystart(2) xyend(2)], 'fontsize', 12);
xlabel('X [mm]', 'fontsize', 14);
ylabel('Y [mm]', 'fontsize', 14);
% zlabel('Index of plane', 'fontsize', 14);
axetitle = regexprep(FILENAME, '_', '\\_');
title(Ha_axes, axetitle);

% draw clk src
line(clksrc(1), clksrc(2), 'Marker', '^', 'Color', tiercolor{clksrc(3)+1}, 'MarkerSize', 10, 'MarkerFaceColor', tiercolor{clksrc(3)+1});

% draw sinks
for ii=1:nsink
    %sink+
    line(xyz(ii,1), xyz(ii,2), ...
    'LineStyle', 'none', 'Marker', 'x', 'Color', tiercolor{xyz(ii,3)+1}, 'MarkerSize', 8);
    %text(xyz(ii,1)+0.01, xyz(ii,2)+0.01, num2str(ii-1));
end

% for ii=1:size(mgpxyz,1)
%     %merge point
%     line(mgpxyz(:,1), mgpxyz(:,2), ...
%         'LineStyle', 'none', 'Marker', '.', 'Color', tiercolor{mgpxyz(ii,3)+1});
% end

% draw manhattan nets
for ii=1:size(netxyz,1)
    pt1 = [netxyz(ii,1) netxyz(ii,2)];
    pt2 = [netxyz(ii,3) netxyz(ii,4)];
    line([pt1(1) pt1(1) pt2(1)], [pt1(2) pt2(2) pt2(2)], ...
        'LineStyle', '-', 'Color', tiercolor{netxyz(ii,5)+1}); 
end

% draw tsvs
for ii=1:size(tsvxyz,1)
    x = tsvxyz(ii,1);
    y = tsvxyz(ii,2);
    z = tsvxyz(ii,3);   % z of the parent node
    line(x, y, ...
        'Marker', 'o', 'MarkerFaceColor', tiercolor{1}, 'MarkerEdgeColor', tiercolor{1}, 'MarkerSize', 4); 
end

% % draw buffers
% for ii=1:size(bufxyz,1)
%     x = bufxyz(ii,1);
%     y = bufxyz(ii,2);
%     z = bufxyz(ii,3);   % z of the parent node
%     line(x, y, ...
%         'Marker', 'd', 'MarkerEdgeColor', tiercolor{z+1}); 
% %     fprintf('buf: %d, %d, %d.\n', x, y, z);
% end

end
